﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;

namespace FrmSocketTest
{
    public class ClientReceiveManage
    {
        public void OparData(string methodName, byte[] requestBody, int errcode)
        {
            try
            {
                //各种错误返回的格式不一致，分别处理
                var messageStatus = CommHelper.EnumHelper.GetEnumObject<CommData.MessageStatus>(CommData.MessageStatus.Error, errcode);
                if (messageStatus == CommData.MessageStatus.Exception)
                {
                    var exceptiondic = CommHelper.NetworkDataHelper.GetNetworkDataHelper.GetJsonObject<Dictionary<string, object>>(requestBody);
                    if (exceptiondic.Count > 0)
                    {
                        CommHelper.LogHelper.WriteErrorLog(string.Format("{0},{1}:{2}", DateTime.Now,
                            methodName, exceptiondic["exception"]));
                    }
                }
                //返回错误数据后的消息触发
                if (CommData.MessageStatus.Success != messageStatus && CommData.MessageStatus.Exception != messageStatus)
                {
                }
                else
                {
                    this.GetType().InvokeMember(methodName, (BindingFlags.InvokeMethod)
                                                            , null, this, new object[] { methodName, requestBody, messageStatus });
                }
            }
            catch (Exception ex)
            {
                CommHelper.LogHelper.WriteErrorLog(ex.Message);
            }
        }

        public void Test(string methodName, byte[] requestBody, CommData.MessageStatus errCode)
        {
            switch (errCode)
            {
                case CommData.MessageStatus.Success:
                    var modelTest = CommHelper.NetworkDataHelper.GetNetworkDataHelper.GetJsonObject<Domain.Entities.OperModel>(requestBody);

                    EventHelper.FunModelData(modelTest);
                    break;
            }
        }
    }
}